package com.cqie.service.impl;

import com.cqie.dto.TokenUserLoginDto;
import com.cqie.dto.medical.MedicalPublicationDto;
import com.cqie.entity.MedicalPublication;
import com.cqie.exception.SpecialException;
import com.cqie.mapper.user.DoctorInfoMapper;
import com.cqie.mapper.medical.MedicalPublicationMapper;
import com.cqie.service.IMedicalPublicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.threadlocal.UserThreadLocal;
import com.cqie.util.RedisUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 公告信息表 服务实现类
 * </p>
 *
 * @author 不吃海鲜
 * @since 2024-12-04
 */
@Service
public class MedicalPublicationServiceImpl extends ServiceImpl<MedicalPublicationMapper, MedicalPublication> implements IMedicalPublicationService {

    @Resource
    private MedicalPublicationMapper medicalPublicationMapper;

    @Resource
    private DoctorInfoMapper doctorInfoMapper;

    private final RedisUtils redisUtils;

    public MedicalPublicationServiceImpl(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 新增 / 更新 公告信息
     * @param medicalPublicationDto
     * @return
     */
    @Override
    public Boolean saveMedicalPublication(MedicalPublicationDto medicalPublicationDto) throws SpecialException {
        // 获取当前操作人id
        TokenUserLoginDto tokenDto = UserThreadLocal.get(TokenUserLoginDto.class);

        // 根据 doctorId 查询 doctorName
        var doctorName = doctorInfoMapper.getDoctorNameByDoctorId(tokenDto.getDoctorId());

        if (ObjectUtils.isEmpty(doctorName)){
            throw new SpecialException("医生信息未填写");
        }

        // 标题不能为空
        if (ObjectUtils.isEmpty(medicalPublicationDto.getPublicationTitle())) {
            throw new SpecialException("标题不能为空");
        }

        // 内容不能为空
        if (ObjectUtils.isEmpty(medicalPublicationDto.getPublicationContent())) {
            throw new SpecialException("内容不能为空");
        }

        // 类型不能为空
        if (ObjectUtils.isEmpty(medicalPublicationDto.getPublicationType())) {
            throw new SpecialException("类型不能为空");
        }

        MedicalPublication medicalPublication = new MedicalPublication();
        BeanUtils.copyProperties(medicalPublicationDto, medicalPublication);
        medicalPublication.setDoctorId(tokenDto.getDoctorId());
        medicalPublication.setDoctorName(doctorName);
        // 新增
        if (ObjectUtils.isEmpty(medicalPublicationDto.getPublicationId())){
            return medicalPublicationMapper.insert(medicalPublication) > 0;
        }
        // 更新
        else {
            return medicalPublicationMapper.updateMedicalByPublicationId(medicalPublication);
        }
    }

    /**
     * 查询公告信息 List
     * @param publicationType 公告类型
     * @return
     */
    @Override
    public List<MedicalPublicationDto> getMedicalPublicationList(String publicationType) {
        return medicalPublicationMapper.getMedicalPublicationList(publicationType);
    }

    /**
     * 根据 publicationId 删除公告信息
     * @param publicationId
     * @return
     */
    @Override
    public Boolean deleteMedicalPublication(Integer publicationId) {
        return medicalPublicationMapper.deleteMedicalPublication(publicationId);
    }
}
